package com.fitz.remote.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public enum DynamicTriggerEvent {

    /**
     * @see HeartbeatClientHandler
     * 心跳事件
     */
    HEARTBEAT_EVENT(HeartbeatClientHandler.class) {
        @Override
        protected ITriggerHandler trigger0(ChannelHandlerContext ctx, Class<?> baseClazz, Object o, Object b) {
            ctx.channel().pipeline().addBefore(baseClazz.getSimpleName() ,IdleStateHandler.class.getSimpleName(), new IdleStateHandler(0, 15, 0, TimeUnit.SECONDS));
            HeartbeatClientHandler htClientHandler = new HeartbeatClientHandler();
            ctx.pipeline().addBefore(baseClazz.getSimpleName(), getClazz().getSimpleName(), htClientHandler);
            return htClientHandler;
        }
    },

    ;

    private Class<? extends ITriggerHandler> clazz;

    DynamicTriggerEvent(Class<? extends ITriggerHandler> clazz) {
        this.clazz = clazz;
    }

    public void trigger(ChannelHandlerContext ctx, Class<?> baseClazz, Object o, Object b) {
        trigger0(ctx, baseClazz, o,b).launch(ctx);
    }

    protected  abstract ITriggerHandler trigger0(ChannelHandlerContext ctx, Class<?> baseClazz, Object o, Object b);

    public Class<?> getClazz() {
        return clazz;
    }


}
